/**============================================================================
 *
 * Author: luo1o1o1o 
 *
 * QQ: 330240995, PHONE: 17600071321
 *
 * Last modified: 2021-06-01 17:39
 *
 * Filename: user.js
 *
 * Description: .user api 路由
 *
 ============================================================================**/
const router = require('koa-router')()
const {
    isExist,
    register,
    login,
    deleteCurUser,
    changeInfo,
    changePassword,
    logout,
} = require('../../controller/user')
const userValidate = require('../../validator/user')
const { genValidator } = require('../../middlewares/validator')
const { isTest } = require('../../utils/env')
const { loginChecks } = require('../../middlewares/loginChecks')

// 前缀
router.prefix('/api/user')
// 注册
router.post('/register', genValidator(userValidate), async (ctx, next) => {
    const { userName, password, gender } = ctx.request.body
    // 调用 controller, 返回
    ctx.body = await register({ userName, password, gender })
})

// 用户名是否存在
router.post('/isExist', async (ctx, next) => {
    const { userName } = ctx.request.body
    // controller
    ctx.body = await isExist(userName)
})

// 登录
router.post('/login', async (ctx, next) => {
    const { userName, password } = ctx.request.body
    ctx.body = await login(ctx, userName, password)
})

router.post('/delete', loginChecks, async (ctx, next) => {
    if (isTest) {
        // 测试环境下，测试账号登录之后，删除自己
        const { userName } = ctx.session.userInfo
        // 调用 controller
        ctx.body = await deleteCurUser(userName)
    }
})

// 修改个人信息
router.patch(
    '/changeInfo',
    loginChecks,
    genValidator(userValidate),
    async (ctx, next) => {
        const { nickName, city, picture } = ctx.request.body
        ctx.body = await changeInfo(ctx, { nickName, city, picture })
    }
)

// 修改密码
router.patch(
    '/changePassword',
    loginChecks,
    genValidator(userValidate),
    async (ctx, next) => {
        const { password, newPassword } = ctx.request.body
        const { userName } = ctx.session.userInfo
        ctx.body = await changePassword(userName, password, newPassword)
    }
)

router.post('/logout', loginChecks, async (ctx, next) => {
    // controller
    ctx.body = await logout(ctx)
})

module.exports = router
